function replacePunctuation(str) -- 定义替换符号
    local punctuation = {
        -- ["【"] = "[",
        -- ["】"] = "]",
        ["（"] = "(",
        ["）"] = ")",
        ["——"] = "--",
        -------------------
        ["“"] = "\"",
        ["”"] = "\"",
        ["‘"] = "\"",
        ["’"] = "\"",
        ["「"] = "\"",
        ["」"] = "\"",
        ["『"] = "\"",
        ["』"] = "\"",
        ----------------
        ["#"] = "",
        ["∉∑"] = "",
        ["*"] = "",
        ["@"] = "",
        ["&"] = "",
        [" "] = "",
        ["　"] = "",
        [" "] = "",
    }
    for CNpunc, ENpunc in pairs(punctuation) do
        str = str:gsub(CNpunc, ENpunc)
    end
    return str
end

function Clipboard_Change_Format() -- 剪贴板转表1 --返回文本
    local characterStyle = { ".-第.-集.-$", ".-第.-章.-$", ".-第.-卷.-$"}
    local binary_table = {}
    local clipboard = reaper.CF_GetClipboard("") -- 获取剪贴板内容

    table.insert(binary_table, "1") -- 开头插入一个 "1"
    for line in clipboard:gmatch("[^\r\n]+") do
        local matched = false
        for _, pattern in ipairs(characterStyle) do
            if line:match(pattern) then
                matched = true
                break
            end
        end
        if matched then
            table.insert(binary_table, "☆ " .. line .. " ☆") -- 如果匹配，则在该行的前后添加 "1"
        else
            table.insert(binary_table, line) -- 如果不匹配，则按原样插入
        end
    end
    table.insert(binary_table, "☆ 文本结束 ☆") -- 末尾添加 "☆ 文本结束 ☆"
    return binary_table
end

function Clipboard_Change_Format2() -- 剪贴板转表2 --返回文本
    local characterStyle = { ".-第.-集.-$", ".-第.-章.-$", ".-第.-卷.-$"}
    local resultString = ""
    local binary_table = { "1" } -- 初始化表并插入第一个 "1"

    local clipboard = reaper.CF_GetClipboard("") -- 获取剪贴板内容
    local clipboard = replacePunctuation(clipboard) -- 替换符号
    local clipboard = clipboard:gsub("\n%s*\n", "\n") -- 删除多余的换行

    for line in clipboard:gmatch("[^\r\n]+") do
        if not line:find("^【.-】【.-】【.-】【.-】.-") then
            local matched = false
            for _, pattern in ipairs(characterStyle) do
                if line:match(pattern) then
                    matched = true
                    break
                end
            end
            if matched then
                line = "☆ " .. line .. " ☆"
            end
            resultString = resultString .. line .. "\n" -- 将处理后的行加入结果字符串
        end
    end

    local resultString = resultString:gsub("【", "\n【") -- 在【之前换行
    local resultString = resultString:gsub("【.-】\".-\"", "%1\n") -- 在引号后面换行
    local resultString = resultString:gsub("\n%s*\n", "\n") -- 删除多余的换行

    for line in resultString:gmatch("[^\r\n]+") do
        table.insert(binary_table, line)
    end
    table.insert(binary_table, "☆ 文本结束 ☆")

    return binary_table
end